﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCB.UI.Utils;
using Baosight.ZNH.ZMCB.UI.PopWindows;

namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 查询中长期计划-主表
    /// </summary>
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 梁琦
    /// Version: 1.0
    /// History:
    ///       2013-07-08  梁琦   [创建]
    ///
    public partial class ZMCBC0 : EFViewCmes
    {
        
        public ZMCBC0()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }
        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (null != this.grid.SelectedItem)
            {
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();

                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdForeignKey", _row["fdKeyId"].ToString());
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCBC1", args);
            }
            else
            {
                this.container.Content = null;
            }
        }

        

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            String fdKeyId = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow()["fdKeyId"].ToString();
            Dictionary<string, string> args = new Dictionary<string, string>();

            args.Add("fdKeyId", fdKeyId);
            Shell.LoadView("ZMCB20", args);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public  void OnCompareButtonClick(object sender, RoutedEventArgs e)
        {
            //EIInfo eiInfo = new EIInfo();
            //IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            
            //EIBlock eIBlock = new EIBlock("result");

            //if (_list.Count < 2)
            //{
            //    //row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            //}

            //eIBlock.SetRows(_list);
            //eiInfo.AddBlock(eIBlock);
            TempButton = sender as Button;
            TempButton.IsEnabled = false;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set(EIConstants.ServiceNameKey, "ZMCBC0");
            eiInfo.Set(EIConstants.MethodNameKey, "compare");
            this.Submit(eiInfo, QueryCompleted);
            //OpenPopup(eiInfo, "比较分析", null, null);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public void QueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            
            //if (e.EIInfo.Status != -1)
            //{
                SE0Compare view = new SE0Compare();
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(e.EIInfo);

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = "比较分析";//设置Title
                _popup.Content = view;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            //}
            //else
            //{
            //    BaseTools.OpenEFWindowAlert("查询失败:" + e.EIInfo.Msg, null);
            //    TempButton.IsEnabled = true;
            //}

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
